package com.netty.client;

import com.nettryhomework.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

public class UserClientHandler extends ChannelInboundHandlerAdapter implements Callable {

    private ChannelHandlerContext ctx;
    private String result;
    private RpcRequest RpcRequest;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        this.ctx = ctx;
    }

    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        result = msg.toString();
        notify();
    }

    @Override
    public synchronized Object call() throws Exception {
        ctx.writeAndFlush(RpcRequest);
        System.out.println("已发送");
        wait();
        return result;
    }

    public void setParam(RpcRequest RpcRequest){
        this.RpcRequest = RpcRequest;
    }
}
